import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import '../utils/kudo_colors.dart';

enum ReportType {
  inappropriateContent('Inappropriate Content', Icons.warning_amber_rounded),
  hateSpeech('Hate Speech', Icons.sentiment_very_dissatisfied),
  disturbingContent('Disturbing Content', Icons.visibility_off),
  illegalActivity('Illegal Activity', Icons.gavel),
  spamAdvertisement('Spam/Advertisement', Icons.block),
  otherIssues('Other Issues', Icons.help_outline),
  blockUser('Block User', Icons.person_off);

  const ReportType(this.displayName, this.icon);
  final String displayName;
  final IconData icon;
}

typedef ReportCallback = void Function(ReportType reportType);

class ReportDialog extends StatefulWidget {
  final ReportCallback onReport;
  final String title;

  const ReportDialog({
    super.key,
    required this.onReport,
    this.title = 'Report',
  });

  static Future<void> show({
    required BuildContext context,
    required ReportCallback onReport,
    String title = 'Report',
  }) {
    return showDialog(
      context: context,
      barrierDismissible: true,
      builder: (context) => ReportDialog(onReport: onReport, title: title),
    );
  }

  @override
  State<ReportDialog> createState() => _ReportDialogState();
}

class _ReportDialogState extends State<ReportDialog>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _scaleAnimation;
  late Animation<double> _fadeAnimation;

  ReportType? _selectedReportType;

  @override
  void initState() {
    super.initState();
    _initializeAnimations();
    _animationController.forward();
  }

  void _initializeAnimations() {
    _animationController = AnimationController(
      duration: const Duration(milliseconds: 300),
      vsync: this,
    );

    _scaleAnimation = Tween<double>(begin: 0.8, end: 1.0).animate(
      CurvedAnimation(parent: _animationController, curve: Curves.easeOutBack),
    );

    _fadeAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(parent: _animationController, curve: Curves.easeOut),
    );
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _animationController,
      builder: (context, child) {
        return FadeTransition(
          opacity: _fadeAnimation,
          child: ScaleTransition(
            scale: _scaleAnimation,
            child: Dialog(
              backgroundColor: Colors.transparent,
              child: _buildDialogContent(),
            ),
          ),
        );
      },
    );
  }

  Widget _buildDialogContent() {
    return Container(
      constraints: BoxConstraints(
        maxWidth: 400.w,
        maxHeight: MediaQuery.of(context).size.height * 0.8,
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20.r),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withValues(alpha: 0.1),
            blurRadius: 20,
            offset: const Offset(0, 10),
          ),
        ],
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          _buildHeader(),
          Flexible(child: _buildReportOptions()),
          _buildActionButtons(),
        ],
      ),
    );
  }

  Widget _buildHeader() {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.all(20.w),
      decoration: BoxDecoration(
        color: Colors.red.withValues(alpha: 0.1),
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(20.r),
          topRight: Radius.circular(20.r),
        ),
      ),
      child: Column(
        children: [
          SizedBox(height: 8.h),
          Text(
            widget.title,
            style: GoogleFonts.lato(
              fontSize: 18.sp,
              fontWeight: FontWeight.bold,
              color: KudoColors.black,
            ),
            textAlign: TextAlign.center,
          ),
        ],
      ),
    );
  }

  Widget _buildReportOptions() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
      child: SingleChildScrollView(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children:
              ReportType.values.map((reportType) {
                final isSelected = _selectedReportType == reportType;
                final isBlockUser = reportType == ReportType.blockUser;

                return Padding(
                  padding: EdgeInsets.only(bottom: 8.h),
                  child: _buildReportOption(
                    reportType,
                    isSelected,
                    isBlockUser,
                  ),
                );
              }).toList(),
        ),
      ),
    );
  }

  Widget _buildReportOption(
    ReportType reportType,
    bool isSelected,
    bool isBlockUser,
  ) {
    return Container(
      decoration: BoxDecoration(
        color:
            isSelected
                ? (isBlockUser
                    ? Colors.red.withValues(alpha: 0.1)
                    : KudoColors.green.withValues(alpha: 0.1))
                : Colors.grey.withValues(alpha: 0.05),
        borderRadius: BorderRadius.circular(12.r),
        border: Border.all(
          color:
              isSelected
                  ? (isBlockUser ? Colors.red : KudoColors.green)
                  : Colors.transparent,
          width: 1.5,
        ),
      ),
      child: ListTile(
        contentPadding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 4.h),
        leading: Icon(
          reportType.icon,
          color:
              isSelected
                  ? (isBlockUser ? Colors.red : KudoColors.green)
                  : Colors.grey,
          size: 24.sp,
        ),
        title: Text(
          reportType.displayName,
          style: GoogleFonts.lato(
            fontSize: 14.sp,
            fontWeight: isSelected ? FontWeight.w600 : FontWeight.w500,
            color:
                isSelected
                    ? (isBlockUser ? Colors.red : KudoColors.green)
                    : KudoColors.black,
          ),
        ),
        trailing:
            isSelected
                ? Icon(
                  Icons.check_circle,
                  color: isBlockUser ? Colors.red : KudoColors.green,
                  size: 20.sp,
                )
                : null,
        onTap: () {
          setState(() {
            _selectedReportType = reportType;
          });
        },
      ),
    );
  }

  Widget _buildActionButtons() {
    return Container(
      padding: EdgeInsets.all(20.w),
      child: Row(
        children: [
          Expanded(child: _buildCancelButton()),
          SizedBox(width: 12.w),
          Expanded(child: _buildSubmitButton()),
        ],
      ),
    );
  }

  Widget _buildCancelButton() {
    return Container(
      height: 44.h,
      decoration: BoxDecoration(
        color: Colors.grey.withValues(alpha: 0.1),
        borderRadius: BorderRadius.circular(12.r),
        border: Border.all(color: Colors.grey.withValues(alpha: 0.3)),
      ),
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          borderRadius: BorderRadius.circular(12.r),
          onTap: () => Navigator.of(context).pop(),
          child: Center(
            child: Text(
              'Cancel',
              style: GoogleFonts.lato(
                fontSize: 14.sp,
                fontWeight: FontWeight.w600,
                color: Colors.grey,
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildSubmitButton() {
    final isEnabled = _selectedReportType != null;
    final isBlockUser = _selectedReportType == ReportType.blockUser;
    final buttonColor =
        isEnabled
            ? (isBlockUser ? Colors.red : KudoColors.green)
            : Colors.grey.withValues(alpha: 0.3);

    return Container(
      height: 44.h,
      decoration: BoxDecoration(
        color: buttonColor,
        borderRadius: BorderRadius.circular(12.r),
      ),
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          borderRadius: BorderRadius.circular(12.r),
          onTap: isEnabled ? _handleSubmit : null,
          child: Center(
            child: Text(
              isBlockUser ? 'Block' : 'Report',
              style: GoogleFonts.lato(
                fontSize: 14.sp,
                fontWeight: FontWeight.bold,
                color: Colors.white,
              ),
            ),
          ),
        ),
      ),
    );
  }

  void _handleSubmit() {
    if (_selectedReportType != null) {
      Navigator.of(context).pop();
      widget.onReport(_selectedReportType!);
    }
  }
}
